package cn.bsb.service.rece;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.common.Mapper;

@Service
@Transactional
public abstract class BaseService<T> {

	@Autowired
	protected Mapper<T> mapper;
	
	public int save(T entity){
        return mapper.insert(entity);
    }
	
	public int update(T entity){
        return mapper.updateByPrimaryKey(entity);
    }
    public int delete(T entity){
        return mapper.deleteByPrimaryKey(entity);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS)
    public T selectByPK(Object key){
    	return mapper.selectByPrimaryKey(key);
    }
    /**
     * 根据实体中的属性值进行查询，查询条件使用等号
     * 当参数entity为null时，等同于selectAll()
     * @param entity
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<T> selectList(T entity){
    	return mapper.select(entity);
    }

    /**
     * 根据实体中的属性值进行分页查询，查询条件使用等号
     * 当参数entity为null时，等同于selectAll()，分页显示结果
     * @param entity
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<T> selectPage(T entity, int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<T> list = mapper.select(entity);
        PageInfo<T> page = new PageInfo<T>(list);
        return page;
    }
    
	/**
	 * 根据主键更新不为null的值
	 * @param entity
	 * @return
	 */
	@Transactional(propagation=Propagation.SUPPORTS)
    public int updateByPKNotNULL(T entity){	
    	return mapper.updateByPrimaryKeySelective(entity);
    }
}
